public class Solution983 {
    public int mincostTickets(int[] days, int[] costs) {
        if (costs[2]<costs[1]){
            costs[1]=costs[2];
        }
        if (costs[1]<costs[0]){
            costs[0]=costs[1];
        }
        int[] f=new int[366];
        f[0]=0;
        int cur=0;
        for (int i=1;i<366;i++){
            if (cur<days.length && days[cur]==i){
                f[i]=f[i-1]+costs[0];
                int t=i-7>=0?f[i-7]:0;
                f[i]=Math.min(t+costs[1],f[i]);
                t=i-30>=0?f[i-30]:0;
                f[i]=Math.min(t+costs[2],f[i]);
                cur++;
            }else{
                f[i]=f[i-1];
            }
        }
        return f[365];
    }

    public static void main(String[] args) {
        System.out.println(new Solution983().mincostTickets(new int[]{1,4,6,7,8,20},new int[]{2,7,15}));
    }
}
